﻿#region The MIT License (MIT)
// The MIT License (MIT)
// Copyright (c) 2010 - 2013 Sovacode LLC
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#endregion

using System.ComponentModel;
using System.Windows;
using Sovacode.RubiksCube.Common.Attributes;

namespace Sovacode.RubiksCube.Common.ViewModel
{
    /// <summary>
    /// View model with common settings for window such as Top, Left etc. It can restore window and save position
    /// </summary>
    [ValidateByManager]
    public class MainViewModel : ViewModelTop
    {
        #region Override

        /// <summary>
        /// Load config file
        /// </summary>
        public override void LoadSettings()
        {
            SizeToFit();
            MoveIntoView();
        }

        #endregion

        #region Window helpers

        /// <summary>
        /// <see cref="WindowState"/>
        /// </summary>
        [Browsable(false)]
        public WindowState WindowState
        {
            get { return SettingsManager.GetSetting(() => WindowState); }
            set
            {
                if (SettingsManager.GetSetting(() => WindowState) == value)
                {
                    return;
                }

                SettingsManager.PutSetting(() => WindowState, value);

                OnPropertyChanged(() => WindowState);
            }
        }

        /// <summary>
        /// Top position of window
        /// </summary>
        [Browsable(false)]
        public virtual double Top
        {
            get { return SettingsManager.GetDoubleSetting(() => Top); }

            set
            {
                SettingsManager.PutSetting(() => Top, value);

                OnPropertyChanged(() => Top);
            }
        }

        /// <summary>
        /// Left position of window
        /// </summary>
        [Browsable(false)]
        public virtual double Left
        {
            get { return SettingsManager.GetDoubleSetting(() => Left); }
            set
            {
                SettingsManager.PutSetting(() => Left, value);

                OnPropertyChanged(() => Left);
            }
        }

        /// <summary>
        /// Height
        /// </summary>
        [Browsable(false)]
        public virtual double Height
        {
            get { return SettingsManager.GetDoubleSetting(() => Height); }
            set
            {
                SettingsManager.PutSetting(() => Height, value);

                OnPropertyChanged(() => Height);
            }
        }

        /// <summary>
        /// Width
        /// </summary>
        [Browsable(false)]
        public virtual double Width
        {
            get { return SettingsManager.GetDoubleSetting(() => Width); }
            set
            {
                SettingsManager.PutSetting(() => Width, value);

                OnPropertyChanged(() => Width);
            }
        }

        /// <summary>
        /// Fit
        /// </summary>
        private void SizeToFit()
        {
            if (Height > SystemParameters.VirtualScreenHeight)
            {
                Height = SystemParameters.VirtualScreenHeight;
            }

            if (Width > SystemParameters.VirtualScreenWidth)
            {
                Width = SystemParameters.VirtualScreenWidth;
            }
        }

        /// <summary>
        /// Back to view
        /// </summary>
        private void MoveIntoView()
        {
            if (Top + Height / 2 > SystemParameters.VirtualScreenHeight)
            {
                Top = SystemParameters.VirtualScreenHeight - Width;
            }

            if (Left + Width / 2 > SystemParameters.VirtualScreenWidth)
            {
                Left = SystemParameters.VirtualScreenWidth - Width;
            }

            if (Top < 0)
            {
                Top = 0;
            }

            if (Left < 0)
            {
                Left = 0;
            }
        }

        #endregion
    }
}